package com.zz.yt.lib.bluetooth.delegate.pad;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.EncodeUtils;
import com.zz.yt.lib.annex.popup.utils.HeadUtils;
import com.zz.yt.lib.bluetooth.R;
import com.zz.yt.lib.ui.listener.OnClickStringListener;
import com.zz.yt.lib.ui.popup.BaseLattePopup;


/**
 * 提示打印条形码
 *
 * @author qf
 * @version 1.0
 **/
public class BarCodePopup extends BaseLattePopup<BarCodePopup> implements View.OnClickListener {

    private String mBarCode = null;

    private OnClickStringListener mClickListener = null;
    private View.OnClickListener mClickCancelListener = null;

    @NonNull
    public static BarCodePopup create(Context context) {
        return new BarCodePopup(context);
    }

    private BarCodePopup(Context context) {
        setContext(context);
        setFocusAndOutsideEnable(false)
                .setBackgroundDimEnable(true)
                .setDimValue(0.5f);
    }

    @Override
    protected void initAttributes() {
        setContentView(R.layout.blu_popup_image_bar_code,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected void initViews(@NonNull View view, BarCodePopup barCodePopup) {
        final ImageView imgBarCode = view.findViewById(R.id.img_bar_code);
        setBarCode(imgBarCode);
        final Button buttonLocal = view.findViewById(R.id.cancel);
        final Button buttonConfirm = view.findViewById(R.id.confirm);
        buttonLocal.setOnClickListener(this);
        buttonConfirm.setOnClickListener(this);
    }

    private void setBarCode(ImageView imgBarCode) {
        HeadUtils.image(imgBarCode, mBarCode);
    }

    @Override
    public void onClick(@NonNull View v) {
        int id = v.getId();
        if (id == R.id.confirm) {
            PrintingUtil.create(mBarCode).setOnClickListener(mClickListener);
        } else if (id == R.id.cancel) {
            if (mClickCancelListener != null) {
                mClickCancelListener.onClick(null);
            }
        }
        dismiss();
    }

    /**
     * @param barCode：设置条形码base64
     */
    public BarCodePopup setBarCode(String barCode) {
        this.mBarCode = PrintingUtil.BASE_JPEG_64 + EncodeUtils.htmlDecode(barCode);
        return this;
    }


    /**
     * @param listener：打印成功回调
     */
    public BarCodePopup setClickListener(OnClickStringListener listener) {
        this.mClickListener = listener;
        return this;
    }

    /**
     * @param listener：取消打印回调
     */
    public BarCodePopup setClickCancelListener(View.OnClickListener listener) {
        this.mClickCancelListener = listener;
        return this;
    }


}
